// pages/takephoto/takephoto.js
import Dialog from 'vant-weapp/dialog/dialog';
const uploadImage = require('../../utils/uploadFile.js');
const client = require("../../utils/client.js");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showsrc: false,
    type: 1,
    showoverlay: true,
    d_type: 0,
  },
  //上传到阿里云
  upHeadImg(img) {
    console.log(img, '传入图片')
    var that = this
    uploadImage(img, `wms-prod2024/`,
      function (res) {
        console.log(res, '上传图片')
        client.callRecognizeBankCard(wx, res, function (result) {
          console.log(result.Data.PersonNumber, '输出结果')
          if (result.Data.PersonNumber !== 1) {
            wx.hideLoading()
            wx.showModal({
              title: '警告',
              content: '未识别到人体姿态，请重新拍照上传！',
              showCancel: 'false',
              complete: (res) => {
                if (res.cancel) {

                }
              }
            })
            return
          } else {
            wx.hideLoading()
            switch (that.data.type) {
              case 1:
                wx.hideLoading()
                that.setData({
                  frontImgUrl: res,
                  type: 2,
                  showsrc: false,
                  showoverlay: true,
                  d_type: 2
                })
                break;
              case 2:
                wx.hideLoading()
                that.setData({
                  sideImgUrl: res,
                  type: 3,
                  showsrc: false,
                  showoverlay: true,
                  d_type: 3
                })
                break;
              case 3:
                wx.hideLoading()
                that.setData({
                  backImgUrl: res,
                })
                var item = {
                  frontImgUrl: '',
                  sideImgUrl: '',
                  backImgUrl: '',
                }
                item.frontImgUrl = that.data.frontImgUrl
                item.sideImgUrl = that.data.sideImgUrl
                item.backImgUrl = that.data.backImgUrl
                console.log(item)
                wx.setStorageSync('userdate', item)
                wx.navigateTo({
                  url: '/pages/index/index',
                })
                break;
              default:
                break;
            }
          }
        }.bind(this));
      },
      function (res) {
        console.log(res, '上传失败')
        wx.hideLoading()
      }
    )
  },
  //点击拍摄
  camera() {
    console.log(11111)
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log(res)
        switch (this.data.type) {
          case 1:
            this.setData({
              showsrc: false,
              src_1: res.tempImagePath,
              showsrc: true,
            })
            break;
          case 2:
            this.setData({
              showsrc: false,
              src_2: res.tempImagePath,
              showsrc: true
            })
            break;
          case 3:
            this.setData({
              src_3: res.tempImagePath,
              showsrc: true
            })
            break;
          default:
            break;
        }
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },
  selectImg() {
    var that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album'],
      camera: 'back',
      success(res) {
        console.log(res)
        switch (that.data.type) {
          case 1:
            that.setData({
              showsrc: false,
              src_1: res.tempFiles[0].tempFilePath,
              showsrc: true,
            })
            break;
          case 2:
            that.setData({
              showsrc: false,
              src_2: res.tempFiles[0].tempFilePath,
              showsrc: true
            })
            break;
          case 3:
            that.setData({
              src_3: res.tempFiles[0].tempFilePath,
              showsrc: true
            })
            break;
          default:
            break;
        }
      }
    })
  },
  //重新拍摄
  return () {
    this.setData({
      src: '',
      showsrc: false
    })
  },
  //点击上传
  upload() {
    switch (this.data.type) {
      case 1:
        wx.showLoading({
          title: '上传中...',
        })
        this.upHeadImg(this.data.src_1)
        break;
      case 2:
        wx.showLoading({
          title: '上传中...',
        })
        this.upHeadImg(this.data.src_2)
        break;
      case 3:
        wx.showLoading({
          title: '上传中...',
        })
        this.upHeadImg(this.data.src_3)
        break;
      default:
        break;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const _this = this
    _this.setData({
      type: 1,
      d_type: 0,
    })
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.camera']) {
          // 用户已经授权
          _this.setData({
            isAuth: true
          })
        } else {
          // 用户还没有授权，向用户发起授权请求
          wx.authorize({
            scope: 'scope.camera',
            success() { // 用户同意授权
              _this.setData({
                isAuth: true
              })
            },
            fail() { // 用户不同意授权
              Dialog.alert({
                title: '提示',
                message: '授权失败，请打开相机授权！！',
              }).then(() => {
                wx.openSetting({
                  success(res) {
                    _this.onLoad()
                  }
                })
              });
              // wx.navigateBack({
              //   data: 1
              // })
            }
          })
        }
      },
      fail: res => {
        console.log('获取用户授权信息失败')
      }
    })
  },
  hideshow() {
    switch (this.data.d_type) {
      case 0:
        var volumeJson = wx.getStorageSync('volumeJson')
        if(volumeJson){
          this.setData({
            showoverlay: false
          })
          Dialog.confirm({
            title: '提示',
            message: '您已拍照测量过量体数据，是否需要重新测算？',
            confirmButtonText: '查看',
            cancelButtonText: '重测'
          })
            .then(() => {
              wx.redirectTo({
                url: '/pages/consequence/consequence',
              })
            })
            .catch(() => {
              this.setData({
                d_type: 1,
                showoverlay: true
              })
            });
        }else{
          this.setData({
            d_type: 1
          })
        }
        break;
      case 1:
        this.setData({
          showoverlay: false
        })
        break;
      case 2:
        this.setData({
          showoverlay: false
        })
        break;
      case 3:
        this.setData({
          showoverlay: false
        })
        break;
      default:
        break;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})